//
//  ViewCalculator.cpp
//  demo
//
//  Created by vonchenchen on 2021/3/19.
//  Copyright © 2021 vonchenchen. All rights reserved.
//

#include "ViewCalculator.hpp"
#include <stdio.h>
#include <cstring>
#include <math.h>

ViewCalculator::ViewCalculator():_rotate_x(0),
                                 _rotate_y(0),
                                 _rotate_z(0){

}

ViewCalculator::~ViewCalculator(){
    
}

void ViewCalculator::InitEyePos(const Vec<3, float> &eye_pos){
    _eye_pos = eye_pos;
    //根据出始位置确定初始条件下绕每个轴旋转的角度
    
    // arctan(y / z) 当前左视角度
    float current_deg = 180*atan(eye_pos[1]/eye_pos[2])/PI;
    if(current_deg < 0){
        //在平面二象限或者第四象限 arctan突变为负数
        if(eye_pos[1]>0 && eye_pos[2]<0){
            current_deg = 90 + abs(current_deg);
        }else if(eye_pos[1]<0 && eye_pos[2]>0){
            current_deg = 270 + abs(current_deg);
        }
    }
 
    //绕y轴 arctan(x / z) 当前右偏角
    current_deg = 180*atan(eye_pos[0]/eye_pos[2])/PI;
    if(current_deg < 0){
        //右手系 zx平面 逆时针为正 由y轴俯视 右下角设为第一象限
        //在平面二象限或者第四象限 arctan突变为负数
        if(eye_pos[0] >0 && eye_pos[2] < 0){
            current_deg = 90 + abs(current_deg);
        }else if(eye_pos[0] <0 && eye_pos[2] > 0){
            current_deg = 270 + abs(current_deg);
        }
    }
    _rotate_y = current_deg;
    
    //arctan(y / x) 绕z轴偏角
    current_deg = 180*atan(eye_pos[1]/eye_pos[0])/PI;
    if(current_deg){
        if(eye_pos[0]<0 && eye_pos[1]>0){
            current_deg = 90 + abs(current_deg);
        }
        if(eye_pos[0]>0 && eye_pos[1]<0){
            current_deg = 270 + abs(current_deg);
        }
    }
}

Vec<3, float> ViewCalculator::RotateX(Vec<3, float> &eye_pos, float add_r_degree){

    //arctan(y / z) 当前俯视角度
    //float current_deg = 180*atan(eye_pos[1]/eye_pos[2])/PI;
    //当前角度加增量角度
    _rotate_x += add_r_degree;
    return Rotate(eye_pos, 0, _rotate_x);
}

Vec<3, float> ViewCalculator::RotateY(Vec<3, float> &eye_pos, float add_r_degree){
    
    //arctan(x / z) 当前右偏角
    //float current_deg = 180*atan(eye_pos[0]/eye_pos[2])/PI;
    _rotate_y += add_r_degree;
    return Rotate(eye_pos, 1, _rotate_y);
}

Vec<3, float> ViewCalculator::RotateZ(Vec<3, float> &eye_pos, float add_r_degree){
    
    //arctan(y / x) 绕z轴偏角
    //float current_deg = 180*atan(eye_pos[1]/eye_pos[0])/PI;
    _rotate_z += add_r_degree;
    return Rotate(eye_pos, 2, _rotate_z);
}

Vec<3, float> ViewCalculator::Rotate(Vec<3, float> &eye_pos, int aix, float r_degree){

    Vec<3, float> vec = eye_pos;
    if(r_degree == 0){
        return vec;
    }
    
    float degree = abs(r_degree);
    while(degree > 360){
        degree = degree - 360.0f;
    }
    
    if(r_degree < 0){
        degree = 360.0f - degree;
    }
    std::cout<<"r_degree="<<r_degree<<" degree="<<degree<<std::endl;
    
    float arc = PI*degree/180.0f;
    if(aix == 0){
        //绕x轴旋转
        float l = sqrt(vec[1]*vec[1] + vec[2]*vec[2]);
        vec[1] = l * sin(arc);
        vec[2] = l * cos(arc);
    }else if(aix == 1){
        //绕y轴旋转
        float l = sqrt(vec[0]*vec[0] + vec[2]*vec[2]);
        vec[0] = l * sin(arc);
        vec[2] = l * cos(arc);
    }else if(aix == 2){
        //绕z轴旋转
        float l = sqrt(vec[0]*vec[0] + vec[1]*vec[1]);
        vec[0] = l * cos(arc);
        vec[1] = l * sin(arc);
    }
    
    return vec;
}
